{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.9 Fourth order equations\n",
    "\n",
    "We consider the Kirchhoff plate equation: Find $w \\in H^2$, such that\n",
    "$$\n",
    "\\int \\nabla^2 w : \\nabla^2 v = \\int f v\n",
    "$$\n",
    "    \n",
    "A conforming method requires $C^1$ continuous finite elements. But there is no good option available, and thus there is no $H^2$ conforming finite element space in NGSolve.\n",
    "\n",
    "We have the following two alternatives:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hybridized $C^0$-continuous interior penalty method:\n",
    "\n",
    "A simple way out is to use continuous elements, and treat the missing $C^1$-continuity by a Discontinuous Galerkin method. A DG formulation is\n",
    "\n",
    "$$\n",
    "\\sum_T \\nabla^2 w : \\nabla^2 v\n",
    "- \\int_{E} \\{\\nabla^2 w\\}_{nn} \\, [\\partial_n v] \n",
    "- \\int_{E} \\{\\nabla^2 v\\}_{nn} \\, [\\partial_n w] + \\alpha \\int_E  [\\partial_n w]  [\\partial_n v] \n",
    "$$\n",
    "\n",
    "[Baker 77, Brenner Gudi Sung, 2010]\n",
    "\n",
    "We consider its hybrid DG version, where the normal derivative is a new, facet-based variable:\n",
    "\n",
    "$$\n",
    "\\sum_T \\nabla^2 w : \\nabla^2 v\n",
    "- \\int_{\\partial T} (\\nabla^2 w)_{nn} \\, (\\partial_n v - \\widehat{v_n})\n",
    "- \\int_{\\partial T} (\\nabla^2 v)_{nn} \\, (\\partial_n w - \\widehat{w_n}) + \\alpha \\int_E (\\partial_n v - \\widehat{v_n}) (\\partial_n w - \\widehat{w_n})\n",
    "$$\n",
    "\n",
    "The facet variable is the normal derivative $n_E \\cdot \\nabla w$, what is oriented along the arbitrarily chosen edge normal-vector. We cannot use the FacetSpace since it does not have the orientation, but we can use the normal traces of an HDiv space. We don't need inner basis functions, so we set order inner to 0:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve import *\n",
    "from ngsolve.webgui import Draw\n",
    "from netgen.geom2d import unit_square\n",
    "mesh = Mesh (unit_square.GenerateMesh(maxh=0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order = 3\n",
    "\n",
    "V1 = H1(mesh, order=order, dirichlet=\"left|bottom|right|top\")\n",
    "# V2 = NormalFacetFESpace(mesh, order=order-1, orderinner=0, dirichlet=\"left|bottom|right|top\")\n",
    "V2 = HDiv(mesh, order=order-1, orderinner=0, dirichlet=\"left|bottom|right|top\")\n",
    "\n",
    "V = FESpace ([V1,V2])\n",
    "\n",
    "w,what = V.TrialFunction()\n",
    "v,vhat = V.TestFunction()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some proxy-functions and gridfunctions provide additional differential operators. We can get them via the Operator function. w.Operator(\"hesse\") provides the Hessian, a matrix-valued function. Note that we can use the InnerProduct(.,.) for $\\nabla^2 w : \\nabla^2 v$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = specialcf.normal(2)\n",
    "h = specialcf.mesh_size\n",
    "\n",
    "def jumpdn(v,vhat): \n",
    "    return n*(grad(v)-vhat)\n",
    "def hesse(v):\n",
    "    return v.Operator(\"hesse\")\n",
    "def hessenn(v):\n",
    "    return InnerProduct(n, hesse(v)*n)\n",
    "\n",
    "dS = dx(element_boundary=True)\n",
    "a = BilinearForm(V)\n",
    "a += InnerProduct (hesse(w), hesse(v)) * dx \\\n",
    "     - hessenn(w) * jumpdn(v,vhat) * dS \\\n",
    "     - hessenn(v) * jumpdn(w,what) * dS \\\n",
    "     + 3*order*order/h * jumpdn(w,what) * jumpdn(v,vhat) * dS\n",
    "a.Assemble()\n",
    "\n",
    "f = LinearForm(V)\n",
    "f += 1*v*dx\n",
    "f.Assemble()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "u = GridFunction(V)\n",
    "u.vec.data = a.mat.Inverse(V.FreeDofs()) * f.vec\n",
    "\n",
    "Draw (u.components[0], mesh, \"disp_DG\")\n",
    "Draw (grad (u.components[0]), mesh, \"grad\")\n",
    "Draw (hesse (u.components[0]), mesh, \"hesse\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Hellan-Herrmann-Johnson Mixed Method:\n",
    "[Hellan 67, Herrmann 67, Johnson 73, Arnold+Brezzi 85, Comodi 89]\n",
    "\n",
    "We introduce the bending moments as a new, matrix valued variable $\\sigma = \\nabla^2 w$. Then we can write the plate equation as a saddle point problem:\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\DeclareMathOperator{\\Div}{div}\n",
    "\\begin{array}{ccccll}\n",
    "\\int \\sigma : \\tau & - & \\int \\tau : \\nabla^2 w & = & 0 & \\forall \\, \\tau \\\\\n",
    "-\\int \\sigma : \\nabla^2 v &  &  & = & \\int f v  & \\forall \\, v\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Integration by parts in upper-right and lower-left terms:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\DeclareMathOperator{\\Div}{div}\n",
    "\\begin{array}{ccccll}\n",
    "\\int \\sigma : \\tau & + & < \\Div \\tau , \\nabla w > & = & 0 & \\forall \\, \\tau \\\\\n",
    "< \\Div \\sigma , \\nabla v >  &  &  & = & \\int f v  & \\forall \\, v\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Hellan-Herrmann-Johnson method uses finite elements $\\sigma_h$ which have continuous normal-normal component. Then, the term $<\\Div \\tau, \\nabla w>$ must be understood in distributional form:\n",
    "\n",
    "$$\n",
    "\\sum_T \\int_T \\Div \\sigma \\, \\nabla v - \\sum_E \\int_E [\\sigma_{nt}] (\\nabla v)_t\n",
    "$$\n",
    "\n",
    "Since $\\sigma_{nn}$ is continuous, the jump occurs only in the tangential component of $\\sigma_n$. Luckily, it hits the tangential component of $\\nabla v$, which is single-valued for $H^1$ finite elements. Thus, we can rewrite the term element-by-element:\n",
    "\n",
    "$$\n",
    "\\sum_T \\int_T \\Div \\sigma \\, \\nabla v -  \\int_{\\partial T} \\sigma_{nt}\\nabla_t v\n",
    "$$\n",
    "\n",
    "The Hellan-Herrmann-Johnson method does not require access to the neighbour element as in DG methods. \n",
    "In [Pechstein-Schöberl 11] the function space $H(\\Div \\Div)$ was introduced. The HHJ finite elements are (nearly) conforming for this space. A basis was also given in [PS11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order = 3\n",
    "\n",
    "V = HDivDiv(mesh, order=order-1) \n",
    "Q = H1(mesh, order=order, dirichlet=\"left|bottom|top|right\")\n",
    "X = FESpace([V,Q])\n",
    "\n",
    "print (\"ndof-V:\", V.ndof, \", ndof-Q:\", Q.ndof)\n",
    "\n",
    "sigma, w = X.TrialFunction()\n",
    "tau, v = X.TestFunction()\n",
    "\n",
    "n = specialcf.normal(2)\n",
    "\n",
    "def tang(u): return u-(u*n)*n\n",
    "\n",
    "a = BilinearForm(X, symmetric=True)\n",
    "a += (InnerProduct (sigma, tau) + div(sigma)*grad(v) \\\n",
    "      + div(tau)*grad(w) - 1e-10*w*v )*dx \\\n",
    "      + (-(sigma*n) * tang(grad(v)) - (tau*n)*tang(grad(w)))*dx(element_boundary=True)\n",
    "a.Assemble()\n",
    "\n",
    "f = LinearForm(X)\n",
    "f += -1 * v * dx\n",
    "f.Assemble()\n",
    "\n",
    "gfu = GridFunction(X)\n",
    "gfu.vec.data = a.mat.Inverse(X.FreeDofs()) * f.vec\n",
    "\n",
    "Draw (gfu.components[0], mesh, name=\"sigma\")\n",
    "Draw (gfu.components[1], mesh, name=\"disp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
